<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryObj.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="hInputChange"
        />
        <el-tree
          ref="tree"
          node-key="id"
          highlight-current
          check-strictly
          :data="empList"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          @current-change="hCurrentChange"
        />
        <!-- 树形组件 -->
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showDialog = true">excel导入</el-button>
          <import-excel :show-dialog.sync="showDialog" />
          <el-button size="mini" @click="hExport">excel导出</el-button>
        </el-row>
        <el-table :data="empdata">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{row}">
              {{ row.formOfEmployment===1? '正式':'非正式' }}
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{row}">
              <el-button size="mini" type="text" @click="$router.push('/employee/detail/'+row.id)">查看</el-button>
              <el-button size="mini" type="text" @click="hAssign(row.id)">角色</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="hdel(row.id)"
              >

                <el-button slot="reference" size="mini" type="text">删除</el-button>
              </el-popconfirm></template>
          </el-table-column>
        </el-table>
        <!-- 表格组件 -->
        <!-- 分页 -->
        <el-row type="flex" style="height:60px" align="middle" justify="end">
          <!-- 放置分页组件 -->
          <el-pagination
            background
            :current-page="queryObj.page"
            :page-sizes="[5,10,15]"
            layout=" prev, pager, next ,sizes"
            :total="total"
            :page-size="queryObj.pagesize"
            @current-change="currentChange"
            @size-change="hSizeChange"
          />
        </el-row>
      </div>
      <el-dialog :visible="isShow" append-to-body @close="isShow=false">
        <template :v-slot="{row}">
          <el-checkbox-group v-model="roleIds">
            <el-checkbox v-for="item in RoleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
          </el-checkbox-group>
          <el-row>
            <div style="margin-top: 20px; text-align: right">
              <el-button type="primary" @click="hSaveAssign">确定</el-button>
              <el-button @click="isShow=false">取消</el-button>
            </div></el-row></template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartment } from '@/api/department'
import { getEmployee, exportEmployee, delEmployee, getEmployeeDetail, AssignRole } from '@/api/employee'
import { getRolesEnabled } from '@/api/role'
import { arrToTree } from '@/utils/index'
import FileSaver from 'file-saver'
// import ImportExcelVue
import ImportExcel from './components/ImportExcel.vue'
export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      curId: -1,
      RoleList: [],
      roleIds: [],
      isShow: false,
      showDialog: false,
      defaultProps: {
        label: 'name'
      },
      empList: [
        { name: '传智教育', children: [
          { name: '总裁办' },
          { name: '行政部' },
          { name: '财务部' }
        ] }
      ],
      queryObj: {
        page: 1,
        pagesize: 10,
        keyword: null,
        departmentId: null

      },
      total: null,
      empdata: []
    }
  },
  created() {
    this.getDepartment()
    this.loadRoles()
  },
  methods: {
    // 分配角色
    async hSaveAssign() {
      await AssignRole({ id: this.curId, roleIds: this.roleIds })
      this.isShow = false
      this.$message.success('分配角色成功')
    },
    // 获取当前员工角色ID
    async  getRoleId() {
      // console.log(1)
      const res = await getEmployeeDetail(this.curId)
      this.roleIds = res.data.roleIds

      console.log(res)
    },
    hAssign(id) {
      this.curId = id
      this.getRoleId()
      this.isShow = true
    },
    // 初始化渲染角色选项
    async   loadRoles() {
      const res = await getRolesEnabled()
      // console.log(res)
      this.RoleList = res.data
    },
    // 删除
    async  hdel(id) {
      await delEmployee(id)
      this.$message.success('删除成功')
      this.loadEmployee()
    },
    // 导出excel表格
    async  hExport() {
      const res = await exportEmployee()
      console.log(res)
      FileSaver.saveAs(res, '员工信息表.xlsx')
    },
    // 搜索框请求函数
    hInputChange() {
      // 清空 定时器ID
      clearTimeout(this.timeId)
      // 防抖函数
      this.timeId = setTimeout(() => {
        this.queryObj.page = 1
        this.loadEmployee()
      }, 500)
    },
    // 切换每页数据条数
    hSizeChange(pages) {
      this.queryObj.pagesize = pages
      this.queryObj.page = 1

      this.loadEmployee()
    },
    // 翻页
    currentChange(page) {
      // alert(page)
      this.queryObj.page = page
      this.loadEmployee()
    },
    async getDepartment() {
      const res = await getDepartment()
      // console.log(res)
      this.empList = arrToTree(res.data, 0)
      const id = this.empList[0].id
      // 初次点击员工板块 的初次渲染获取ID
      this.queryObj.departmentId = id
      this.$nextTick(() => {
        this.$refs.tree.setCheckedKeys([id])
      })
      // 初次点击员工板块 的初次渲染
      this.loadEmployee()
    },
    // 获取员工数据请求
    async loadEmployee() {
      const res = await getEmployee(this.queryObj)
      // console.log(res)
      this.total = res.data.total
      this.empdata = res.data.rows
    },
    // 点击部门名称请求部门员工数据函数
    hCurrentChange(data, node) {
      // console.log(data)
      this.queryObj.departmentId = data.id
      // this.queryObj.keyword = data.keyword
      this.loadEmployee()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
<style>
.el-tree--highlight-current .el-tree-node.is-checked > .el-tree-node__content {
    color: #276FFB;
    background-color: #f0f4ff;
}
</style>
